//
//  SmallDecisionsSzViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/12.
//

#import "SmallDecisionsSzViewController.h"
#import "SmallDecisionsSzView.h"

@interface SmallDecisionsSzViewController ()<CAAnimationDelegate>
@property (nonatomic, strong) SmallDecisionsSzView *szView;
@property (nonatomic, strong) UIImageView *szBottomImageView;
@property (nonatomic, strong) UIImageView *szTopImageView;
@property (nonatomic, strong) UIButton *szStartBtn;
@property (nonatomic, strong) UIImageView *szAnimationImageView;

@end

@implementation SmallDecisionsSzViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"摇骰子";
    [self szBottomImageView];
    [self szAnimationImageView];
    [self szView];
    [self szTopImageView];
    [self szStartBtn];
}


- (void)szStartBtnAction {
    @weakify(self);
    if([self.szStartBtn.titleLabel.text isEqualToString:@"摇一摇"]) {
        [self startShakingAnimation];
    } else if([self.szStartBtn.titleLabel.text isEqualToString:@"开骰"]) {
        [UIView animateWithDuration:0.35 animations:^{
            [weak_self.szTopImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(weak_self.szBottomImageView).offset(-200);
            }];
        }];
        [self.szStartBtn setTitle:@"再来一局" forState:UIControlStateNormal];
    } else {
        [self.szTopImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.szBottomImageView).offset(-25);
        }];
        self.szBottomImageView.hidden = YES;
        self.szTopImageView.hidden = YES;
        self.szAnimationImageView.hidden = NO;
        [self startShakingAnimation];
        [self.szView startDisplay];
        [self.szStartBtn setTitle:@"开骰" forState:UIControlStateNormal];
    }
    
}
  
- (void)startShakingAnimation {
    // 先左右晃动
    CABasicAnimation *shakeLeftRight = [CABasicAnimation animationWithKeyPath:@"position.x"];
    shakeLeftRight.duration = 0.1;
    shakeLeftRight.repeatCount = 10;
    shakeLeftRight.autoreverses = YES;
    shakeLeftRight.fromValue = @(self.szAnimationImageView.center.x - 30);
    shakeLeftRight.toValue = @(self.szAnimationImageView.center.x + 30);
    // 然后上下晃动
    CABasicAnimation *shakeUpDown = [CABasicAnimation animationWithKeyPath:@"position.y"];
    shakeUpDown.duration = 0.1;
    shakeUpDown.repeatCount = 10;
    shakeUpDown.autoreverses = YES;
    shakeUpDown.fromValue = @(self.szAnimationImageView.center.y - 40);
    shakeUpDown.toValue = @(self.szAnimationImageView.center.y + 40);
    // 创建动画组
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[shakeLeftRight, shakeUpDown];
    animationGroup.duration = 1.0; // 总持续时间
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    // 设置动画完成后的回调
    animationGroup.delegate = self;
    
    [self.szAnimationImageView.layer addAnimation:animationGroup forKey:@"shakeAnimation"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        // 动画完成后显示随机骰子值
        [self.szAnimationImageView.layer removeAllAnimations];
        self.szBottomImageView.hidden = NO;
        self.szTopImageView.hidden = NO;
        self.szAnimationImageView.hidden = YES;
        [self.szStartBtn setTitle:@"开骰" forState:UIControlStateNormal];
    }
}

    

- (UIImageView *)szAnimationImageView {
    if(!_szAnimationImageView) {
        _szAnimationImageView = [[UIImageView alloc] init];
        _szAnimationImageView.image = [UIImage imageNamed:@"saizhong_shake_img"];
        [self.view addSubview:_szAnimationImageView];
        [_szAnimationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(60);
            make.right.mas_offset(-60);
            make.height.equalTo(self.szAnimationImageView.mas_width).multipliedBy(1.2);
            make.centerX.mas_offset(0);
            make.bottom.equalTo(self.szBottomImageView);
        }];
    }
    return _szAnimationImageView;
}

- (UIImageView *)szTopImageView {
    if(!_szTopImageView) {
        _szTopImageView = [[UIImageView alloc] init];
        _szTopImageView.image = [UIImage imageNamed:@"saizhong_build_wait_img"];
        _szTopImageView.hidden = YES;
        _szTopImageView.userInteractionEnabled = YES;
        [self.view addSubview:_szTopImageView];
        UITapGestureRecognizer *panGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapSzTopImageViewAction)];
        [_szTopImageView addGestureRecognizer:panGes];
        [_szTopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.centerX.equalTo(self.szBottomImageView);
            make.bottom.equalTo(self.szBottomImageView).offset(-25);
            make.height.mas_equalTo(self.szTopImageView.mas_width).multipliedBy(1.2);
        }];
    }
    return _szTopImageView;
}

- (void)tapTapSzTopImageViewAction {
    [self lookSzResult];
}

- (UIImageView *)szBottomImageView {
    if(!_szBottomImageView) {
        _szBottomImageView = [[UIImageView alloc] init];
        _szBottomImageView.hidden = YES;
        _szBottomImageView.image = [UIImage imageNamed:@"saizhong_end_img"];
        [self.view addSubview:_szBottomImageView];
        [_szBottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(60);
            make.right.mas_offset(-60);
            make.height.mas_equalTo(self.szBottomImageView.mas_width).multipliedBy(0.6);
            make.centerX.mas_offset(0);
            make.centerY.mas_offset(40);
        }];
    }
    return _szBottomImageView;
}

- (UIButton *)szStartBtn {
    if(!_szStartBtn) {
        _szStartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_szStartBtn setTitle:@"摇一摇" forState:UIControlStateNormal];
        [_szStartBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_szStartBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _szStartBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(20);
        _szStartBtn.layer.cornerRadius = 30;
        _szStartBtn.layer.masksToBounds = YES;
        _szStartBtn.backgroundColor = SmallDecisionsThemeColor;
        [_szStartBtn addTarget:self action:@selector(szStartBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_szStartBtn];
        [_szStartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(60);
            make.width.mas_offset(260);
            make.centerX.mas_offset(0);
            make.top.equalTo(self.szBottomImageView.mas_bottom).offset(80);
        }];
    }
    return _szStartBtn;
}

- (SmallDecisionsSzView *)szView {
    if(!_szView) {
        _szView = [[SmallDecisionsSzView alloc] initWithDiceCount:self.diceCount];
        [self.szBottomImageView addSubview:_szView];
        [_szView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_offset(0);
            make.top.mas_offset(20);
            make.left.mas_offset(40);
            make.right.bottom.mas_offset(-40);
        }];
    }
    return _szView;
}


- (UIView *)setRightBarButton {
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"看一眼" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
    [rightBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    return rightBtn;
}

- (void)rightBarButtonAction:(UIButton *)view {
    [self lookSzResult];
}

- (void)lookSzResult {
    @weakify(self);
    [UIView animateWithDuration:0.35 animations:^{
        [weak_self.szTopImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weak_self.szBottomImageView).offset(-200);
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weak_self.szTopImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(weak_self.szBottomImageView).offset(-25);
            }];
        });
    }];
}

- (BOOL)setHideNavBar {
    return YES;
}

@end
